package com.liu.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    public User selectByName(String username) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from t_user where username = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            resultSet = statement.executeQuery();

            if (resultSet.next()) {
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setImageUrl(resultSet.getString("image"));
                user.setEmail(resultSet.getString("email"));
                user.setLocation(resultSet.getString("location"));
                user.setResume(resultSet.getString("resume"));
                user.setAge(resultSet.getString("age"));
                user.setProfession(resultSet.getString("profession"));
                return user;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    public User selectById(int userId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from t_user where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setImageUrl(resultSet.getString("image"));
                user.setEmail(resultSet.getString("email"));
                user.setLocation(resultSet.getString("location"));
                user.setResume(resultSet.getString("resume"));
                user.setAge(resultSet.getString("age"));
                user.setProfession(resultSet.getString("profession"));
                return user;
            }
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    public User register(String username, String password) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "insert into t_user values (null, ?, ?, '暂无', '暂无', '暂无', '暂无', '暂无', '头像.jpg')";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            statement.executeUpdate();
            User user = selectByName(username);
            return user;

        } catch (SQLException throwables) {
            throwables.printStackTrace();;
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    public void editInfo(int userId, String email, String age, String location, String profession, String resume) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            System.out.println(1);
            connection = DBUtil.getConnection();
            String sql = "update t_user set email = ?, age = ?, location = ?, profession = ?, resume = ? where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, email);
            statement.setString(2, age);
            statement.setString(3, location);
            statement.setString(4, profession);
            statement.setString(5, resume);
            statement.setInt(6, userId);
            statement.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();;
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
    }

    public void uploadImageURL(String imageURL, int userId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            System.out.println(10);
            connection = DBUtil.getConnection();
            String sql = "update t_user set image = ? where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, imageURL);
            statement.setInt(2, userId);
            statement.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();;
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
    }
}
